const Monitor = require('../../utils/monitor.js').Monitor
const util = require('../../utils/util.js')

Page(Monitor.hookApp({
  data: {
    waysArr:[],
    qingdan: `${getApp().globalData.imgUrl}jsy-newshop清单.svg`,
    wancheng: `${getApp().globalData.imgUrl}jsy-newshop已完成复制.svg`,
    dengdai: `${getApp().globalData.imgUrl}jsynewshop等待.svg`,
    imgUrls: getApp().globalData.imgUrls,
    heXiao:false     // 核销的状态
  },
  onLoad(o){
    console.log(o)
    if (o.heXiao === 'false'){
      this.setData({
        heXiao: false,
      })  
    }else{
      this.setData({
        heXiao: true,
      }) 
    }
    this.setData({
      order_id: o.order_id,
    })  
  },
  onShow(){
    this.geiPageInfo()
  },
  copyText(e) { //点击复制
    wx.setClipboardData({
      data: e.currentTarget.dataset.text,
      success: function (res) {
        wx.getClipboardData({
          success: function (res) {
            wx.showToast({
              title: '复制成功'
            })
          }
        })
      }
    })
  },
  specifications(trg) { //商品规格转换
    let len = trg.length
    for (let i = 0; i < len; i++) {
      let size = ''
      let str = `${trg[i].sku_type}`
      trg[i].sku_type = JSON.parse(str)
      for (let j = 0; j < trg[i].sku_type.length; j++) {
        for (let prop in trg[i].sku_type[j]) {
          size += trg[i].sku_type[j][prop] + " / "
        }
      }
      trg[i].size = size
      trg[i].size = trg[i].size.substring(0, trg[i].size.length - 3)
    }
    return trg
  },
  geiPageInfo(){
    util.API('pyapi/oder_if', {
      order_id: this.data.order_id
    }, 'application/x-www-form-urlencoded').then(res => {
      let { code, data, orders_goods_list, address, address_info, comment, distribution_name} = res
      if (code == 200) {
        let date = util.getTimeDian(data.ctime),
          date1 = util.getTimeDian(data.paytime);
        data.ctime = `${date.y}.${date.m}.${date.d} ${date.h}:${date.i}:${date.s}`
        data.ptime = `${date1.y}.${date1.m}.${date1.d} ${date1.h}:${date1.i}:${date1.s}`
        orders_goods_list = this.specifications(orders_goods_list)
        let waysArr = this.setWaysArr([], this.data.distributiontype, JSON.stringify(distribution_name))
        this.setData({
          waysArr,
          comment,
          data,
          orders_goods_list,
          address,
          address_info
        })
      }
    })
  },
  // let waysArr = this.setWaysArr([], this.data.distributiontype, JSON.stringify(distribution_name))
  setWaysArr(waysArr, distribution_type, distribution_name) {  // 设置自定义配送信息
    distribution_type = distribution_type.split(',');
    distribution_name = JSON.parse(distribution_name)
    let len = distribution_type.length
    for (let i = 0; i < len; i++) {
      waysArr.push(distribution_name[distribution_type[i]])
    }
    return waysArr
  },
  shaidan(e) {
    wx.navigateTo({
      url: `../Shaidan/Shaidan?order_id=${e.currentTarget.dataset.order_id}`,
    })
  }

}))